<?php
namespace app\payclass\model;

use app\BaseModel;
use think\facade\Db;

class Order extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_payclass_order';


    protected $append = [

        'order_goods',

        'class',

        'create_time_text'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-12-21 13:49
     * @功能说明:
     */
    public function getCreateTimeTextAttr($value,$data){


        if(!empty($data['create_time'])){

            return date('Y-m-d H:i:s',$data['create_time']);
        }
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 15:33
     * @功能说明:
     */
    public function getClassAttr($value,$data){

        if(!empty($data['id'])){

            $order_goods_model = new OrderGoods();

            $data = $order_goods_model->where(['order_id'=>$data['id']])->value('title');

            return $data;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-08 15:30
     * @功能说明:
     */
    public function getOrderGoodsAttr($value,$data){

        if(!empty($data['id'])){

            $order_goods_model = new OrderGoods();

            $data = $order_goods_model->where(['order_id'=>$data['id']])->select()->toArray();

            return $data;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function orderList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_payclass_order_goods b','a.id = b.order_id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:后台列表
     */
    public function adminOrderList($dis,$page){

        $data = $this->alias('a')
            ->join('longbing_card_payclass_order_goods b','a.id = b.order_id')
            ->join('longbing_card_user c','a.user_id = c.id','left')
            ->join('longbing_card_user_phone d','a.user_id = d.user_id','left')
            ->where($dis)
            ->field('a.*,c.nickName,d.phone as mobile ')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function orderInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function orderAdd($data){

        $data['create_time'] = time();


        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function orderUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-15 11:26
     * @功能说明:订单的回调
     */
    public function orderResult($order_code,$transaction_id=''){

        $order = $this->orderInfo(['order_code'=>$order_code,'pay_type'=>1]);

        if(!empty($order)){
            //修改订单状态
            $this->orderUpdate(['id'=>$order['id']],['pay_type'=>2,'transaction_id'=>$transaction_id,'pay_time'=>time()]);

            $auth_model = new Auth();

            foreach ($order['order_goods'] as $v){
                //增加权限
                $insert = [

                    'uniacid' => $order['uniacid'],

                    'user_id' => $order['user_id'],

                    'class_id'=> $v['class_id'],

                    'status'  => 1,

                    'type'    => !empty($order['is_atv'])?1:0

                ];

                $auth_model->authAdd($insert);
            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-22 16:16
     * @功能说明:检测订单过期
     */
    public function check($uniacid){

        $dis[] = ['uniacid','=',$uniacid];

        $dis[] = ['pay_type','=',1];

        $dis[] = ['over_time','<',time()];

        $this->orderUpdate($dis,['pay_type'=>-1]);

        return true;
    }















}